package com.anhtuan.reminder.fragments;

import java.util.ArrayList;
import java.util.List;

import com.anhtuan.reminder.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;

public class IncomingCallFragment extends Fragment implements OnClickListener {
	private EditText et_contact;
	private ImageButton bt_browser_contact;
	
	public static final int CONTACT_PICKER = 0;
	private static final String TAG = "IncomingCallFragment";
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.callme, container, false);
		et_contact = (EditText) view.findViewById(R.id.et_callme_contact);
		bt_browser_contact = (ImageButton) view.findViewById(R.id.bt_callme_browser_contact);
		bt_browser_contact.setOnClickListener(this);
		
		return view;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case CONTACT_PICKER:
				Cursor cursor = null;
				String phoneNumber = "";
				List<String> allNumbers = new ArrayList<String>();
				int phoneIdx = 0;
				
				try {
					Uri result = data.getData();
					String id = result.getLastPathSegment();
					cursor = getActivity().getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] {id}, null);
					phoneIdx = cursor.getColumnIndex(Phone.DATA);
					if (cursor.moveToFirst()) {
						do {
							phoneNumber = cursor.getString(phoneIdx);
							allNumbers.add(phoneNumber);
						} while (cursor.moveToNext());
					}
				} catch (Exception e) {
					Log.e(TAG, "Unable to get contact", e);
				} finally {
					if (cursor != null) {
						cursor.close();
					}
					
					final CharSequence[] items = allNumbers.toArray(new String[allNumbers.size()]);
					AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
					builder.setTitle("Choose a number");
					builder.setItems(items, new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							String selectedNumber = items[which].toString();
							selectedNumber = selectedNumber.replace("-", "");
							selectedNumber = selectedNumber.replace("(", "");
							selectedNumber = selectedNumber.replace(")", "");
							selectedNumber = selectedNumber.replace(" ", "");
							et_contact.setText(selectedNumber);
						}
					});
					
					AlertDialog alert = builder.create();
					
					if (allNumbers.size() > 1) {
						alert.show();
					} else {
						String selectedNumber = phoneNumber.toString();
						selectedNumber = selectedNumber.replace("-", "");
						selectedNumber = selectedNumber.replace("(", "");
						selectedNumber = selectedNumber.replace(")", "");
						selectedNumber = selectedNumber.replace(" ", "");
						et_contact.setText(selectedNumber);
					}
					
					if (phoneNumber.length() == 0) {
						// No number found, nothing to do
					}
				}
				
				break;

			default:
				break;
			}
		}
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		int id = view.getId();
		switch (id) {
		case R.id.bt_callme_browser_contact:
			startPickerActivity(CONTACT_PICKER);
			break;

		default:
			break;
		}
	}
	
	private void startPickerActivity(int requestCode) {
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
		startActivityForResult(intent, requestCode);
	}
}
